package com.core;

import com.domain.CircularQueueV3;
import com.domain.Function.Run;
import com.domain.Task;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 描述 每秒循环一次
 * 主要参考:http://mp.weixin.qq.com/s/eDMV25YqCPYjxQG-dvqSqQ
 * 时间 2017-10-23 20:20
 * 作者 肖亦鸣
 */
public class DelayCore {

    private static volatile DelayCore delayCore;
    private static final CircularQueueV3 CIRCULAR_QUEUE_V3;
    private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private DelayCore() {
    }

    static {
        delayCore = new DelayCore();
        CIRCULAR_QUEUE_V3 = new CircularQueueV3();
    }

    public static DelayCore getInstance() {
        return delayCore;
    }

    public void init() {
        System.out.println("init...");
        //    doDelay();
        doDelayV2();
    }

    private void doDelay() {

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println();
                System.out.println();
                System.out.println("doDelay:" + format.format(new Date()));
                Set<Task> set = CIRCULAR_QUEUE_V3.get();
                System.out.println(set);
                //遍历环形队列中的set集合
                Iterator<Task> iterator = set.iterator();
                while (iterator.hasNext()) {
                    final Task task = iterator.next();
                    if (CIRCULAR_QUEUE_V3.isRun(task)) {
                        task.getFunction().execute(new Run() {

                            public void run() {
                                System.out.println("执行任务:" + task);

                                //执行完了记得移除任务
                                iterator.remove();
                            }
                        });
                    }
                }
            }
        };
        Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(runnable, 0, 1L, TimeUnit.SECONDS);
    }


    private void doDelayV2() {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                System.out.println();
                System.out.println();
                System.out.println("doDelay:" + format.format(new Date()));
                Set<Task> set = CIRCULAR_QUEUE_V3.get();
                System.out.println(set);
                //遍历环形队列中的set集合
                Iterator<Task> iterator = set.iterator();
                while (iterator.hasNext()) {
                    final Task task = iterator.next();
                    if (CIRCULAR_QUEUE_V3.isRun(task)) {
                        task.getFunction().execute(new Run() {

                            public void run() {
                                System.out.println("执行任务:" + task);

                                //执行完了记得移除任务
                                iterator.remove();
                            }
                        });
                    }
                }
            }
        }, 0, 1000);
    }

    public void add(Task task) {
        CIRCULAR_QUEUE_V3.add(task);
    }
}
